package v;

import c.GridTableController;
import com.ezware.oxbow.swingbits.table.filter.TableRowFilterSupport;
import core.constructor.constructor;

import java.awt.Color;
import java.awt.Font;
import javax.swing.Icon;
import javax.swing.JTable;
import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.table.TableColumnExt;
import tools.TextPrompt;

public class UsuariosAdmin_View_Grid extends javax.swing.JPanel {

    constructor c;
    GridTableController configTable;
    private String sqlSelect;
    private String nombreColumnas;

    public UsuariosAdmin_View_Grid(Object... e) {
        initComponents();
        config(e);

        //tabla = (JXTable) TableRowFilterSupport.forTable(new JTable()).searchable(true).actions(false).apply();
        //System.out.println(Class.class.getName());
        configTable = new GridTableController(tabla);
        tabla = configTable.setTabla(tabla, getSqlSelect(), getNombreColumnas(), c);
        configTable.setFilter(jSearch);
        tabla.setName("Usuarios");
        setEstilo();
        // tabla = TableRowFilterSupport.forFilter(new JXTableFilter(new JXTable())).searchable(true).apply();

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabla = (JXTable) TableRowFilterSupport.forTable(new JXTable()).searchable(true).actions(true).apply();
        jSearch = new javax.swing.JTextField();

        jButton1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jButton1.setText("Agregar Usuario");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        tabla.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tabla);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(16, 16, 16)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 975, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(jButton1)
                        .add(18, 18, 18)
                        .add(jSearch, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 246, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButton1)
                    .add(jSearch, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 25, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 485, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    c.constructor("UsuariosAdmin_View_Add", "INSERT", "");
}//GEN-LAST:event_jButton1ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jSearch;
    private org.jdesktop.swingx.JXTable tabla;
    // End of variables declaration//GEN-END:variables

    /**
     * <strong>config</strong><br> Configura los parametros que provienen del
     * constructor de la clase.<br> e[0] - contiene el JFrame 'padre'.
     *
     * @param e
     */
    private void config(Object... e) {
        if (e.length > 0) {
            c = (constructor) e[0];
        }

        //Asignar SQL para Tabla
        setSqlSelect("SELECT u.nombre||' '||u.apellido,u.email,u.usuario,r.nombre,est.nombre FROM usuario u, rol r , estado_usuario est WHERE u.id_rol = r.id AND u.estado = est.id");
        //Asignar Nombre a Columnas Tabla
        setNombreColumnas("NOMBRE,E-MAIL,USUARIO,PERFIL,ESTADO,Consultar,Modificar,Eliminar");

        //Configurar tabla BD
        System.setProperty("tabla_bd", "usuario");

        /*
         * Configurar Accion Boton Eliminar
         * UPDATE = Cambiar de Estado a Inactivo (0)
         * DELETE = ELiminar de la Tabla el Registro
         */
        System.setProperty("accion_eliminar", "UPDATE");
       
    }

    public String getNombreColumnas() {
        return nombreColumnas;
    }

    public void setNombreColumnas(String nombreColumnas) {
        this.nombreColumnas = nombreColumnas;
    }

    public String getSqlSelect() {
        return sqlSelect;
    }

    public void setSqlSelect(String sqlSelect) {
        this.sqlSelect = sqlSelect;
    }

    private void setEstilo() {
        TextPrompt placeHolder = new TextPrompt("Buscar Usuarios", jSearch);
        placeHolder.setForeground(Color.blue);
        placeHolder.changeAlpha(0.5f);
        placeHolder.changeStyle(Font.BOLD + Font.ITALIC);
        placeHolder.setIcon(new javax.swing.ImageIcon(getClass().getResource("/core/img/search24.gif")));
    }
}
